//
//  ZAddPhotosCell.m
//  NoWait
//
//  Created by zhuang zhang on 2020/4/15.
//  Copyright © 2020 zhuang zhang. All rights reserved.
//

#import "ZAddPhotosCell.h"
#import "ZAddPhotosItemCell.h"
#import "ZPhotosItemCell.h"

@interface ZAddPhotosCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UIView *funBackView;
@property (nonatomic,strong) UICollectionView *iCollectionView;
@property (nonatomic,strong) NSArray <ZBaseUnitModel*>*channelList;
@end

@implementation ZAddPhotosCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [super setupView];
    self.contentView.backgroundColor = adaptAndDarkColor([UIColor colorGrayBG], [UIColor colorGrayBGDark]);
    
    
    [self.contentView addSubview:self.funBackView];
    [self.funBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(CGFloatIn750(0));
        make.right.equalTo(self.contentView.mas_right).offset(CGFloatIn750(-0));
        make.top.equalTo(self.contentView.mas_top).offset(CGFloatIn750(0));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(CGFloatIn750(-0));
    }];
    
    [self.funBackView addSubview:self.iCollectionView];
    [self.iCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(self.funBackView);
    }];
}


#pragma mark -Getter
- (UIView *)funBackView {
    if (!_funBackView) {
        _funBackView = [[UIView alloc] init];
        _funBackView.layer.masksToBounds = YES;
        _funBackView.clipsToBounds = YES;
        _funBackView.backgroundColor = adaptAndDarkColor([UIColor whiteColor], [UIColor colorBlackBGDark]);
        ViewRadius(_funBackView, CGFloatIn750(16));
        ViewShadowRadius(_funBackView, CGFloatIn750(10), CGSizeMake(2, 2), 0.5, [UIColor colorGrayBG]);
    }
    return _funBackView;
}

- (UICollectionView *)iCollectionView {
    if (!_iCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing= 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(CGFloatIn750(222), CGFloatIn750(148));
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _iCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        [_iCollectionView setShowsVerticalScrollIndicator:NO];
        [_iCollectionView setShowsHorizontalScrollIndicator:NO];
        _iCollectionView.scrollEnabled = NO;
        [_iCollectionView setBounces:NO];
        _iCollectionView.clipsToBounds = YES;
        [_iCollectionView registerClass:[ZAddPhotosItemCell class] forCellWithReuseIdentifier:[ZAddPhotosItemCell className]];
        [_iCollectionView registerClass:[ZPhotosItemCell class] forCellWithReuseIdentifier:[ZPhotosItemCell className]];
        [_iCollectionView setBackgroundColor:adaptAndDarkColor([UIColor colorWhite],[UIColor colorBlackBGDark])];
        _iCollectionView.delegate = self;
        _iCollectionView.dataSource = self;
    }
    
    return _iCollectionView;
}


#pragma mark collectionview delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.channelList.count < [self.model.uid intValue]) {
        return self.channelList.count + 1;
    }
    return _channelList.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (self.channelList.count < [self.model.uid intValue]) {
        if (indexPath.row == self.channelList.count) {
            ZAddPhotosItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ZAddPhotosItemCell className] forIndexPath:indexPath];
            cell.model = self.model;
            cell.handelBlock = ^(NSInteger index) {
                if (weakSelf.menuBlock) {
                    weakSelf.menuBlock(indexPath.row,YES);
                }
            };
            return cell;
            
        }else{
            ZPhotosItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ZPhotosItemCell className] forIndexPath:indexPath];
            ZBaseUnitModel *model = _channelList[indexPath.row];
            cell.model = model;
            cell.delBlock = ^{
                if (weakSelf.menuBlock) {
                    weakSelf.menuBlock(indexPath.row,NO);
                }
            };
            cell.seeBlock = ^{
                if (weakSelf.seeBlock) {
                    weakSelf.seeBlock(indexPath.row);
                }
            };
            return cell;
        }
    }else{
        ZPhotosItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ZPhotosItemCell className] forIndexPath:indexPath];
        ZBaseUnitModel *model = _channelList[indexPath.row];
        cell.model = model;
        cell.delBlock = ^{
            if (weakSelf.menuBlock) {
                weakSelf.menuBlock(indexPath.row,NO);
            }
        };
        cell.seeBlock = ^{
            if (weakSelf.seeBlock) {
                weakSelf.seeBlock(indexPath.row);
            }
        };
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.menuBlock) {
        self.menuBlock(indexPath.row,YES);
    }
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(CGFloatIn750(0), CGFloatIn750(28), CGFloatIn750(40), CGFloatIn750(28));
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return CGFloatIn750(12);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return CGFloatIn750(0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(CGFloatIn750(222), CGFloatIn750(148));
}


#pragma mark 类型
- (void)setModel:(ZBaseMenuModel *)model {
    _model = model;
    self.channelList = model.units;
}

-(void)setChannelList:(NSArray <ZBaseUnitModel *>*)channelList {
    _channelList = channelList;
    [self.iCollectionView reloadData];
}

+(CGFloat)z_getCellHeight:(id)sender {
    NSArray *list = sender;
    if (!list || list.count == 0) {
        return CGFloatIn750(148) + CGFloatIn750(32);
    }
    NSInteger count = list.count;
    if (count < 9) {
        count += 1;
    }
    if (count%3 > 0) {
        return (count/3 + 1) * CGFloatIn750(148) + ((count/3) * CGFloatIn750(12))+ CGFloatIn750(20);
    }
    return count/3  * CGFloatIn750(148) + (count/3 - 1)  * CGFloatIn750(12) + CGFloatIn750(20);
}

@end




